package com.gl.task;

import com.gl.mapper.UserMapper;
import com.gl.pojo.entity.UserCount;
import com.gl.service.UserCountService;
import com.gl.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;

@Component
public class ScheduledTasks {

    // 每 5 秒执行一次（固定频率）
//    @Scheduled(fixedRate = 5000)
//    public void taskWithFixedRate() {
//        System.out.println("固定频率任务执行: " + LocalDateTime.now());
//    }

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserCountService userCountService;

    // 每天 10:30 执行（Cron 表达式）
    @Scheduled(cron = "0 30 19 * * ?", zone = "Asia/Shanghai")
    public void taskWithCron() {
        // 获取用户表的用户数量
        Integer count = userMapper.count();
        // 获取当前时间
        LocalDate now = LocalDate.now();

        // 封装
        UserCount userCount = new UserCount();
        userCount.setCount(count);
        userCount.setDate(now);
        // 更新
        userCountService.insertUserCount(userCount);
        // 将数量存入数据库用户数量记录表
        System.out.println("Cron 定时任务执行: " + LocalDate.now());
    }
}